<?php
//获取图片名
function getName($picNew,$nameArray){
	foreach ($picNew as $key=>$item){
		foreach ($nameArray[$key] as $key2=>$item2){
			if(in_array($item2['url'], $item)){
				$picNew2[$key][$key2]['url']=$item2['url'];
				$picNew2[$key][$key2]['name']=$item2['name'];
			}
		}
	}
	return $picNew2;
}
//两数组对比，得到第一个数字中第二个数组没有的
function getNew($array1,$array2){
	foreach ($array1 as $key=>$item){
		//var_dump($files_find2[$key]);
		if($array2[$key]){
			if( array_diff($item,$array2[$key]) ){
				$picNew[$key] = array_diff($item,$array2[$key]);
				$gtDay[]=$key;
			}
		}else{
			$picNew[$key] = $item;
			$gtDay[]=$key;
		}
	}
	return array($picNew,$gtDay);
}
//文件扩展名
function extend($file_name)
{
	$extend = pathinfo($file_name);
	$extend = strtolower($extend["extension"]);
	return $extend;
}
//时间对比
function timeCompare($dateStr){
	if(strtotime(date("Y-m-d")) <= strtotime($dateStr) ) return true;
	//if(strtotime(date("Y-m-d",time())) <= strtotime($dateStr) ) return true;
	else return false;
}
//读取两层目录的文件
function read_dir($dir, $array = array()){
	$dh = opendir($dir);
	$files = array();
	while (($file = readdir($dh)) !== false) {
		$file = autoiconv2($file);
		if($file !== '.' && $file !== '..' && !in_array($file, $array) && extend($file)!='txt') {
			//$files[$file] = $file;
			if(timeCompare($file)){
				$dh2 = opendir($dir.$file);
				$i=0;
				while (($file2 = readdir($dh2)) !== false) {
					$file2 = autoiconv2($file2);
					if($file2 !== '.' && $file2 !== '..' && !in_array($file2, $array) && in_array(extend($file2), array('txt')) ) {
						$txtContent[$file] = getTXTContent($dir.$file.'/'.$file2);
					}
					if($file2 !== '.' && $file2 !== '..' && !in_array($file2, $array) && in_array(extend($file2), array('png','jpg','gif','PNG','JPG','GIF')) ) {
						$filesForRs[$file][$i]['url'] = str_replace('../', '', $dir).$file.'/'.$file2;
						$picName = rtrim($file2,'.'.extend($file2));
						$picName = rtrim($picName,'.'.strtoupper(extend($file2)));
						$filesForRs[$file][$i]['name'] = $picName;
						$files[$file][] = str_replace('../', '', $dir).$file.'/'.$file2;
						$i++;
					}
				}
			}
		}
	}
	return array($files,$filesForRs,$txtContent);
}
//新文件内容
function getNewContent($picNew2,$contentArray){
	foreach ($picNew2 as $key=>$item){
		if($contentArray[$key]){
			foreach ($contentArray[$key] as $key2=>$item2){
				if(isset($item2[0]) && isset($item2[2])){
					foreach ($item as $key3=>$item3){
						if ($item3['name']==$item2[0]){
							$picNew2[$key][$key3]['content']=$item2[2];
							$picNew2[$key][$key3]['updater']=$item2[1];
						}
					}
				}
			}
		}
	}
	return $picNew2;
}
//获取文件内容
function getTXTContent($txtname = 'content.txt'){
	$handle = @fopen ( $txtname , "r" );
	if ($handle) {
		while ( ! feof ( $handle ) ) {
			$buffer = fgets ( $handle );
			$charSet = mb_detect_encoding($buffer, array('GB2312','GBK','UTF-16','UCS-2','UTF-8','BIG5','ASCII'));
			$name = autoiconv($buffer,$charSet);
			$parameters[] = array( 0 => substr($name,0,2),2=> substr($name,2) );
		}
		fclose ( $handle );
	}
	return $parameters;
}
function autoiconv($str,$type = "gb2312//IGNORE"){
	if($str){
		return iconv($type,"utf-8", $str);
	}
}
function autoiconv2($str){
	if($str){
		$charSet = mb_detect_encoding($str, array('GB2312','GBK','UTF-16','UCS-2','UTF-8','BIG5','ASCII'));
		return iconv($charSet,"utf-8", $str);
	}
}
function csubstr($str, $start=0, $length=100, $charset="utf-8", $suffix=true)
{
	$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
	$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
	$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
	$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
	preg_match_all($re[$charset], $str, $match);
	
	return $str;
}
?>